package com.archive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.archive.common.CommonEnum;
import com.archive.common.ResponseBean;
import com.archive.common.ResultUtil;
import com.archive.common.ToolsUtils;
import com.archive.dto.PageDTO;
import com.archive.entity.SysFaculty;
import com.archive.entity.SysMajor;
import com.archive.service.SysFacultyService;
import com.archive.service.SysMajorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 专业表 前端控制器
 * </p>
 *
 * @author Janus
 * @since 2023-12-15
 */
@RestController
@RequestMapping("/major")
public class SysMajorController {

    private static  final Logger logger= LoggerFactory.getLogger(SysMajorController.class);

    @Autowired
    private SysMajorService majorService;


    @Autowired
    private SysFacultyService facultyService;

    /**
     * 添加
     * @param major
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public ResponseBean addMajor(SysMajor major, MultipartFile file){
        if(major==null){
            return  ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        QueryWrapper<SysMajor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",major.getName());
        List<SysMajor> checkList = majorService.list(queryWrapper);
        if(checkList!=null&&!checkList.isEmpty()){
            return ResultUtil.error("名称已存在！");
        }
        //设置学院名称
        SysFaculty faculty = facultyService.getById(major.getFacultyId());
        major.setFacultyName(faculty.getName());

        majorService.save(major);
        return  ResultUtil.success();
    }


    /**
     * 修改
     * @param major
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public ResponseBean updateById( SysMajor major) {
        if (major == null) {
            return ResultUtil.error(CommonEnum.BAD_PARAM);
        }
        QueryWrapper<SysMajor> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",major.getName()).ne("id",major.getId());
        List<SysMajor> checkList = majorService.list(queryWrapper);
        if(checkList!=null&&!checkList.isEmpty()){
            return ResultUtil.error("名称已存在！");
        }
        //设置学院名称
        SysFaculty faculty = facultyService.getById(major.getFacultyId());
        major.setFacultyName(faculty.getName());

        //设置更新时间
        major.setUpdateTime(LocalDateTime.now());
        boolean i = majorService.updateById(major);
        return ResultUtil.success() ;
    }

    /**
     * 分页列表查询
     * @param queryParam 查询参数
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseBean getList(PageDTO pageDTO, SysMajor queryParam , HttpSession session) {
        QueryWrapper<SysMajor> queryWrapper=new QueryWrapper<>();

        queryWrapper.like(!StringUtils.isEmpty(queryParam.getName()),"name",queryParam.getName());
        queryWrapper.like(!StringUtils.isEmpty(queryParam.getFacultyName()),"faculty_name",queryParam.getFacultyName());
        queryWrapper.eq(queryParam.getFacultyId()!=null,"faculty_id",queryParam.getFacultyId());
        queryWrapper.orderBy(true,pageDTO.getIsAsc().equals("asc"), ToolsUtils.camelToUnderline(pageDTO.getOrderByColumn()));
        IPage<SysMajor> indexPage = new Page<>(pageDTO.getPageNum(),pageDTO.getPageSize());
        IPage<SysMajor> listPage = majorService.page(indexPage, queryWrapper);
        //获取分页信息
        Map pageInfo = ToolsUtils.wrapperPageInfo(listPage);
        return ResultUtil.successData(pageInfo);
    }

    /**
     * 删除
     * @param idList
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResponseBean delete(@RequestParam("idList") List<Long> idList){
        if(idList==null||idList.isEmpty()){
            return ResultUtil.error("ID不合法！");
        }
        majorService.removeByIds(idList);
        return ResultUtil.success();
    }


}
